<?php
/**
 * Created by PhpStorm.
 * User: dongxiaofeng
 * Date: 2017/3/16
 * Time: 15:56
 */
namespace zhtfrontend\modules\sitepadapi\controllers;

use common\components\helpers\Fun;
use common\components\helpers\MsgeventData;
use common\models\datas\Message;
use yii\base\Event;
use yii\base\ModelEvent;
use zhtfrontend\models\datas\ClassManage;
use zhtfrontend\modules\sitepadapi\components\rest\ActiveController;
use yii\data\ArrayDataProvider;
use yii\db\Query;
use yii\filters\Cors;
use yii\web\Response;

class MonitorController extends ActiveController
{
    //const EVENT_OPEN_MONTH = 'remindClassbeforeMonth';
    public $modelClass = 'common\models\datas\message';

    public $serializer = [
        'class' => 'zhtfrontend\modules\sitepadapi\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        $behaviors['corsFilter'] = [
            'class' => Cors::className(),
            'cors' => [
                'Origin' => ['*'],
                'Access-Control-Allow-Origin' => ['*'],
                'Access-Control-Request-Method' => ['POST', 'PUT', 'GET', 'HEAD', 'OPTIONS'],
                'Access-Control-Request-Headers' => ['Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With'],
                'Access-Control-Allow-Credentials' => true,
                'Access-Control-Max-Age' => 3600,
                'Access-Control-Expose-Headers' => ['X-Pagination-Current-Page'],
            ],
        ];
        //$new_result_arr = (new MsgeventData(['className'=>get_class($this)]))->getBehaviorArray();
        return $behaviors;
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }

    //提前一个月开课进行消息的发送
    public function actionOpenclassmonth(){
        /*判断当前时间，然后找出classmanage中时间在当前时间的前一个月的时间*/
        $current_date = date('Y-m-d',time());//'1518019201'
        $monthBefore = date('Y-m-d',strtotime("$current_date + 30 day"));
        $before = strtotime($monthBefore.' 00:00:01');
        $end = strtotime($monthBefore.' 23:59:59');
        //$res = (new Query())->select('*')->from('{{%class_manage}}')->where(['is_old' => 0, 'is_delete' => 0, 'type' => 1,'status'=>1])->andWhere(['>=', 'start_time', $before])->andWhere(['<=', 'start_time', $end])->all(Fun::getSiteDb());
        $res = ClassManage::find()->where(['is_old' => 0, 'is_delete' => 0, 'type' => 1,'status'=>1])->andWhere(['>=', 'start_time', $before])->andWhere(['<=', 'start_time', $end])->all();
        if(!empty($res)){
            foreach ($res as $re){
                $re->afterFinda('month');
            }
        }
        return ['statusCode' => 200];
    }

    //提前一个周开课进行消息的发送
    public function actionOpenclassweek(){
        $current_date = date('Y-m-d',time());//'1518019201'
        $monthBefore = date('Y-m-d',strtotime("$current_date + 1 week"));
        $before = strtotime($monthBefore.' 00:00:01');
        $end = strtotime($monthBefore.' 23:59:59');
        $res = ClassManage::find()->where(['is_old' => 0, 'is_delete' => 0, 'type' => 1,'status'=>1])->andWhere(['>=', 'start_time', $before])->andWhere(['<=', 'start_time', $end])->all();
        if(!empty($res)){
            foreach ($res as $re){
                $re->afterFinda('week');
            }
        }
        return ['statusCode' => 200];
    }

    //提前一天开课进行消息的发送
    public function actionOpenclassday(){
        $current_date = date('Y-m-d',time());//'1518019201'
        $monthBefore = date('Y-m-d',strtotime("$current_date + 1 day"));
        $before = strtotime($monthBefore.' 00:00:01');
        $end = strtotime($monthBefore.' 23:59:59');
        $res = ClassManage::find()->where(['is_old' => 0, 'is_delete' => 0, 'type' => 1,'status'=>1])->andWhere(['>=', 'start_time', $before])->andWhere(['<=', 'start_time', $end])->all();
        if(!empty($res)){
            foreach ($res as $re){
                $re->afterFinda('day');
            }
        }
        return ['statusCode' => 200];
    }

    //结课前一个月进行提醒
    public function actionEndclassmonth(){
        $current_date = date('Y-m-d',time());//'1518019201'
        $monthBefore = date('Y-m-d',strtotime("$current_date + 30 day"));
        $before = strtotime($monthBefore.' 00:00:01');
        $end = strtotime($monthBefore.' 23:59:59');
        $res = ClassManage::find()->where(['is_old' => 0, 'is_delete' => 0, 'type' => 1,'status'=>1])->andWhere(['>=', 'end_time', $before])->andWhere(['<=', 'end_time', $end])->all();
        if(!empty($res)){
            foreach ($res as $re){
                $re->afterFinda('endmonth');
            }
        }
        return ['statusCode' => 200];
    }
}